/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.Funcionario;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.FolhaPagamento;
import entidade.persistencia.Manutencao;
import entidade.persistencia.Usuario;
import entidade.persistencia.Venda;
import java.util.Collection;
import entidade.persistencia.Compra;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "funcionario")
public class FuncionarioConverter {
    private Funcionario entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of FuncionarioConverter */
    public FuncionarioConverter() {
        entity = new Funcionario();
    }

    /**
     * Creates a new instance of FuncionarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public FuncionarioConverter(Funcionario entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getFuncionarioId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getManutencaoCollection();
        getCompraCollection();
        getUsuarioId();
        getVendaCollection();
        getFolhaPagamentoCollection();
    }

    /**
     * Creates a new instance of FuncionarioConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public FuncionarioConverter(Funcionario entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for funcionarioId.
     *
     * @return value for funcionarioId
     */
    @XmlElement
    public Integer getFuncionarioId() {
        return (expandLevel > 0) ? entity.getFuncionarioId() : null;
    }

    /**
     * Setter for funcionarioId.
     *
     * @param value the value to set
     */
    public void setFuncionarioId(Integer value) {
        entity.setFuncionarioId(value);
    }

    /**
     * Getter for dataAdmissao.
     *
     * @return value for dataAdmissao
     */
    @XmlElement
    public Date getDataAdmissao() {
        return (expandLevel > 0) ? entity.getDataAdmissao() : null;
    }

    /**
     * Setter for dataAdmissao.
     *
     * @param value the value to set
     */
    public void setDataAdmissao(Date value) {
        entity.setDataAdmissao(value);
    }

    /**
     * Getter for dataNascimento.
     *
     * @return value for dataNascimento
     */
    @XmlElement
    public Date getDataNascimento() {
        return (expandLevel > 0) ? entity.getDataNascimento() : null;
    }

    /**
     * Setter for dataNascimento.
     *
     * @param value the value to set
     */
    public void setDataNascimento(Date value) {
        entity.setDataNascimento(value);
    }

    /**
     * Getter for cbo.
     *
     * @return value for cbo
     */
    @XmlElement
    public String getCbo() {
        return (expandLevel > 0) ? entity.getCbo() : null;
    }

    /**
     * Setter for cbo.
     *
     * @param value the value to set
     */
    public void setCbo(String value) {
        entity.setCbo(value);
    }

    /**
     * Getter for ctps.
     *
     * @return value for ctps
     */
    @XmlElement
    public String getCtps() {
        return (expandLevel > 0) ? entity.getCtps() : null;
    }

    /**
     * Setter for ctps.
     *
     * @param value the value to set
     */
    public void setCtps(String value) {
        entity.setCtps(value);
    }

    /**
     * Getter for serie.
     *
     * @return value for serie
     */
    @XmlElement
    public String getSerie() {
        return (expandLevel > 0) ? entity.getSerie() : null;
    }

    /**
     * Setter for serie.
     *
     * @param value the value to set
     */
    public void setSerie(String value) {
        entity.setSerie(value);
    }

    /**
     * Getter for cpf.
     *
     * @return value for cpf
     */
    @XmlElement
    public String getCpf() {
        return (expandLevel > 0) ? entity.getCpf() : null;
    }

    /**
     * Setter for cpf.
     *
     * @param value the value to set
     */
    public void setCpf(String value) {
        entity.setCpf(value);
    }

    /**
     * Getter for rg.
     *
     * @return value for rg
     */
    @XmlElement
    public String getRg() {
        return (expandLevel > 0) ? entity.getRg() : null;
    }

    /**
     * Setter for rg.
     *
     * @param value the value to set
     */
    public void setRg(String value) {
        entity.setRg(value);
    }

    /**
     * Getter for orgaoExpedicao.
     *
     * @return value for orgaoExpedicao
     */
    @XmlElement
    public String getOrgaoExpedicao() {
        return (expandLevel > 0) ? entity.getOrgaoExpedicao() : null;
    }

    /**
     * Setter for orgaoExpedicao.
     *
     * @param value the value to set
     */
    public void setOrgaoExpedicao(String value) {
        entity.setOrgaoExpedicao(value);
    }

    /**
     * Getter for deficiente.
     *
     * @return value for deficiente
     */
    @XmlElement
    public Boolean getDeficiente() {
        return (expandLevel > 0) ? entity.getDeficiente() : null;
    }

    /**
     * Setter for deficiente.
     *
     * @param value the value to set
     */
    public void setDeficiente(Boolean value) {
        entity.setDeficiente(value);
    }

    /**
     * Getter for numeroDependente.
     *
     * @return value for numeroDependente
     */
    @XmlElement
    public Integer getNumeroDependente() {
        return (expandLevel > 0) ? entity.getNumeroDependente() : null;
    }

    /**
     * Setter for numeroDependente.
     *
     * @param value the value to set
     */
    public void setNumeroDependente(Integer value) {
        entity.setNumeroDependente(value);
    }

    /**
     * Getter for mae.
     *
     * @return value for mae
     */
    @XmlElement
    public String getMae() {
        return (expandLevel > 0) ? entity.getMae() : null;
    }

    /**
     * Setter for mae.
     *
     * @param value the value to set
     */
    public void setMae(String value) {
        entity.setMae(value);
    }

    /**
     * Getter for pai.
     *
     * @return value for pai
     */
    @XmlElement
    public String getPai() {
        return (expandLevel > 0) ? entity.getPai() : null;
    }

    /**
     * Setter for pai.
     *
     * @param value the value to set
     */
    public void setPai(String value) {
        entity.setPai(value);
    }

    /**
     * Getter for naturalidade.
     *
     * @return value for naturalidade
     */
    @XmlElement
    public String getNaturalidade() {
        return (expandLevel > 0) ? entity.getNaturalidade() : null;
    }

    /**
     * Setter for naturalidade.
     *
     * @param value the value to set
     */
    public void setNaturalidade(String value) {
        entity.setNaturalidade(value);
    }

    /**
     * Getter for nacionalidade.
     *
     * @return value for nacionalidade
     */
    @XmlElement
    public String getNacionalidade() {
        return (expandLevel > 0) ? entity.getNacionalidade() : null;
    }

    /**
     * Setter for nacionalidade.
     *
     * @param value the value to set
     */
    public void setNacionalidade(String value) {
        entity.setNacionalidade(value);
    }

    /**
     * Getter for dataUltimaAlteracao.
     *
     * @return value for dataUltimaAlteracao
     */
    @XmlElement
    public Date getDataUltimaAlteracao() {
        return (expandLevel > 0) ? entity.getDataUltimaAlteracao() : null;
    }

    /**
     * Setter for dataUltimaAlteracao.
     *
     * @param value the value to set
     */
    public void setDataUltimaAlteracao(Date value) {
        entity.setDataUltimaAlteracao(value);
    }

    /**
     * Getter for manutencaoCollection.
     *
     * @return value for manutencaoCollection
     */
    @XmlElement
    public ManutencaoesConverter getManutencaoCollection() {
        if (expandLevel > 0) {
            if (entity.getManutencaoCollection() != null) {
                return new ManutencaoesConverter(entity.getManutencaoCollection(), uri.resolve("manutencaoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for manutencaoCollection.
     *
     * @param value the value to set
     */
    public void setManutencaoCollection(ManutencaoesConverter value) {
        entity.setManutencaoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for compraCollection.
     *
     * @return value for compraCollection
     */
    @XmlElement
    public ComprasConverter getCompraCollection() {
        if (expandLevel > 0) {
            if (entity.getCompraCollection() != null) {
                return new ComprasConverter(entity.getCompraCollection(), uri.resolve("compraCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for compraCollection.
     *
     * @param value the value to set
     */
    public void setCompraCollection(ComprasConverter value) {
        entity.setCompraCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for usuarioId.
     *
     * @return value for usuarioId
     */
    @XmlElement
    public UsuarioConverter getUsuarioId() {
        if (expandLevel > 0) {
            if (entity.getUsuarioId() != null) {
                return new UsuarioConverter(entity.getUsuarioId(), uri.resolve("usuarioId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for usuarioId.
     *
     * @param value the value to set
     */
    public void setUsuarioId(UsuarioConverter value) {
        entity.setUsuarioId((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for vendaCollection.
     *
     * @return value for vendaCollection
     */
    @XmlElement
    public VendasConverter getVendaCollection() {
        if (expandLevel > 0) {
            if (entity.getVendaCollection() != null) {
                return new VendasConverter(entity.getVendaCollection(), uri.resolve("vendaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for vendaCollection.
     *
     * @param value the value to set
     */
    public void setVendaCollection(VendasConverter value) {
        entity.setVendaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for folhaPagamentoCollection.
     *
     * @return value for folhaPagamentoCollection
     */
    @XmlElement
    public FolhaPagamentoesConverter getFolhaPagamentoCollection() {
        if (expandLevel > 0) {
            if (entity.getFolhaPagamentoCollection() != null) {
                return new FolhaPagamentoesConverter(entity.getFolhaPagamentoCollection(), uri.resolve("folhaPagamentoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for folhaPagamentoCollection.
     *
     * @param value the value to set
     */
    public void setFolhaPagamentoCollection(FolhaPagamentoesConverter value) {
        entity.setFolhaPagamentoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Funcionario entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Funcionario getEntity() {
        if (entity.getFuncionarioId() == null) {
            FuncionarioConverter converter = UriResolver.getInstance().resolve(FuncionarioConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Funcionario entity.
     *
     * @return an resolved entity
     */
    public Funcionario resolveEntity(EntityManager em) {
        List<Manutencao> manutencaoCollection = entity.getManutencaoCollection();
        List<Manutencao> newmanutencaoCollection = new java.util.ArrayList<Manutencao>();
        for (Manutencao item : manutencaoCollection) {
            newmanutencaoCollection.add(em.getReference(Manutencao.class, item.getManutencaoId()));
        }
        entity.setManutencaoCollection(newmanutencaoCollection);
        List<Compra> compraCollection = entity.getCompraCollection();
        List<Compra> newcompraCollection = new java.util.ArrayList<Compra>();
        for (Compra item : compraCollection) {
            newcompraCollection.add(em.getReference(Compra.class, item.getCompraId()));
        }
        entity.setCompraCollection(newcompraCollection);
        Usuario usuarioId = entity.getUsuarioId();
        if (usuarioId != null) {
            entity.setUsuarioId(em.getReference(Usuario.class, usuarioId.getUsuarioId()));
        }
        List<Venda> vendaCollection = entity.getVendaCollection();
        List<Venda> newvendaCollection = new java.util.ArrayList<Venda>();
        for (Venda item : vendaCollection) {
            newvendaCollection.add(em.getReference(Venda.class, item.getVendaId()));
        }
        entity.setVendaCollection(newvendaCollection);
        List<FolhaPagamento> folhaPagamentoCollection = entity.getFolhaPagamentoCollection();
        List<FolhaPagamento> newfolhaPagamentoCollection = new java.util.ArrayList<FolhaPagamento>();
        for (FolhaPagamento item : folhaPagamentoCollection) {
            newfolhaPagamentoCollection.add(em.getReference(FolhaPagamento.class, item.getFolhaPagamentoId()));
        }
        entity.setFolhaPagamentoCollection(newfolhaPagamentoCollection);
        return entity;
    }
}
